﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BillManager.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="BillManager" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Business
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;

    using Hisaab.Business.Entities;
    using Hisaab.Business.Interfaces;
    using Hisaab.Common;
    using Hisaab.Data.Interfaces;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    ///  Defines the <seealso cref="BillManager"/> type.
    /// </summary>
    public class BillManager : IBillManager
    {
        /// <summary>
        /// Member variable _billRepository
        /// </summary>
        private readonly IBillRepository _billRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="BillManager"/> class.
        /// </summary>
        public BillManager()
            : this(ServiceLocator.Current.GetInstance<IBillRepository>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BillManager"/> class.
        /// </summary>
        /// <param name="billRepository">The Bill repository.</param>
        public BillManager(IBillRepository billRepository)
        {
            Contract.Requires<ArgumentNullException>(billRepository != null);

            _billRepository = billRepository;
        }

        #region Implementation of IManagerBase<Bill>

        /// <summary>
        /// Gets the filtered.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IEnumerable<Bill> List(Bill filter = null, int? pageNo = null, int? pageSize = null)
        {
            return _billRepository.Query(filter.Map<Bill, Data.Entities.Bill>(), pageNo, pageSize)
                .ToList()
                .Select(x => x.Map<Data.Entities.Bill, Bill>());
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>The id of entity being added.</returns>
        public int Add(Bill entity)
        {
            return _billRepository.Add(entity.Map<Bill, Data.Entities.Bill>());
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Update(Bill entity)
        {
            _billRepository.Update(entity.Map<Bill, Data.Entities.Bill>());
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(Bill entity)
        {
            _billRepository.Delete(entity.Map<Bill, Data.Entities.Bill>());
        }

        #endregion Implementation of IManagerBase<Bill>

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="id">The id of Bill</param>
        /// <returns>Bill matching by id</returns>
        public Bill GetById(int id)
        {
            return _billRepository.FindById(id).Map<Data.Entities.Bill, Bill>();
        }

        /// <summary>
        /// Gets the name of the by.
        /// </summary>
        /// <param name="billName">Name of the Bill.</param>
        /// <returns>Bill matching by name</returns>
        public Bill GetByName(string billName)
        {
            return _billRepository.Query(new Data.Entities.Bill { Name = billName })
                  .FirstOrDefault()
                  .Map<Data.Entities.Bill, Bill>();
        }
    }
}